<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!-- 分类树 -->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="categoryName"
            placeholder="请输入分类名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="categoryData"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            highlight-current
            accordion
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!-- 列表 -->
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px">
          <el-form-item label="分类编码" prop="categoryCode">
            <el-input
              v-model="queryParams.categoryCode"
              placeholder="请输入分类编码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="分类名称" prop="categoryName">
            <el-input
              v-model="queryParams.categoryName"
              placeholder="请输入分类名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="显示状态" prop="showFlag">
            <el-input
              v-model="queryParams.showFlag"
              placeholder="请输入显示状态"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['art:artCategory:add']"
            >新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['art:artCategory:edit']"
            >修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['art:artCategory:remove']"
            >删除
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['art:artCategory:export']"
            >导出
            </el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="artCategoryList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center"/>
          <el-table-column label="分类图片" align="center" prop="categoryImg">
            <template slot-scope="scope">
              <image-preview :src="scope.row.categoryImg" width="60px" height="60px"/>
            </template>
          </el-table-column>
          <el-table-column label="分类编码" align="center" prop="categoryCode"/>
          <el-table-column label="分类名称" align="center" prop="categoryName"/>
          <el-table-column label="分类层级" align="center" prop="categoryLevel"/>
          <el-table-column label="显示状态" align="center" prop="showFlag">
            <template slot-scope="scope">
              {{ scope.row.showFlag === 1 ? '显示' : '隐藏' }}
            </template>
          </el-table-column>
          <el-table-column label="显示排序" align="center" prop="showSort"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['art:artCategory:edit']"
              >修改
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['art:artCategory:remove']"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改文章分类管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="父分类" prop="pid">
          <!--<el-input v-model="form.pid" placeholder="请输入父分类"/>-->
          <tree-select
            v-model="form.pid"
            :options="categoryData"
            :normalizer="normalizer"
            :show-count="true"
            placeholder="选择上级菜单"
          />
        </el-form-item>
        <el-form-item label="分类编码" prop="categoryCode">
          <el-input v-model="form.categoryCode" placeholder="请输入分类编码"/>
        </el-form-item>
        <el-form-item label="分类名称" prop="categoryName">
          <el-input v-model="form.categoryName" placeholder="请输入分类名称"/>
        </el-form-item>
        <el-form-item label="分类图片" prop="categoryImg">
          <image-upload v-model="form.categoryImg" :limit="1" :file-size="100" auth="N" />
        </el-form-item>
        <el-form-item label="显示状态" prop="showFlag">
          <el-switch v-model="form.showFlag" :active-value="1" :inactive-value="0"/>
        </el-form-item>
        <el-form-item label="显示排序" prop="showSort">
          <el-input-number v-model="form.showSort" controls-position="right" :precision="0" :min="1" :max="999999"></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import TreeSelect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {
  listArtCategory,
  getArtCategory,
  delArtCategory,
  addArtCategory,
  updateArtCategory,
  listArtCategoryAll
} from "@/api/art/artCategory";

export default {
  name: "ArtCategory",
  components: {TreeSelect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文章分类管理表格数据
      artCategoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        categoryCode: null,
        categoryName: null,
        categoryLevel: null,
        pid: null,
        pids: null,
        categoryImg: null,
        showFlag: null,
        showSort: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        categoryCode: [
          {required: true, message: "分类编码不能为空", trigger: "blur"}
        ],
        categoryName: [
          {required: true, message: "分类名称不能为空", trigger: "blur"}
        ],
      },
      defaultProps: {
        children: "children",
        label: "categoryName"
      },
      categoryName: '',
      categoryData: [],
      // 菜单树选项
      menuOptions: [],
    };
  },
  created() {
    this.getList();
    this.getTreeSelect();
  },
  methods: {
    /** 查询文章分类管理列表 */
    getList() {
      this.loading = true;
      listArtCategory(this.queryParams).then(response => {
        this.artCategoryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        categoryCode: null,
        categoryName: null,
        categoryLevel: null,
        pid: null,
        pids: null,
        categoryImg: null,
        showFlag: null,
        showSort: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加文章分类";
      // 新增默认显示
      this.form.showFlag = 1;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getArtCategory(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改文章分类";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateArtCategory(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addArtCategory(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除文章分类管理编号为"' + ids + '"的数据项？').then(function () {
        return delArtCategory(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('art/artCategory/export', {
        ...this.queryParams
      }, `artCategory_${new Date().getTime()}.xlsx`)
    },
    /** 筛选节点*/
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    /** 节点点击触发 */
    handleNodeClick(target) {
      // 查询子节点
      this.queryParams.pageNum = 1;
      this.queryParams.pid = target.id;
      this.getList();
    },
    /** 查询文章分类管理下拉树结构 */
    getTreeSelect() {
      listArtCategoryAll({}).then(res => {
        if (res.code === 200) {
          this.categoryData = [];
          const data = { id: 0, categoryName: '文章分类', level: 0, children: [] };
          data.children = this.handleTree(res.data, "id", "pid");
          this.categoryData.push(data);
        }
      });
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.categoryName,
        children: node.children
      };
    },
  }
};
</script>
